import Vue from "vue";
import { mapState } from "vuex";
import { Base64 } from "@/util/xc/utils";
import { GetSubCompanyDataView } from "@/hooks/update";
import webUrl from "@/config/otherLink";

export default {
  data() {
    return {
      NODE_ENV: process.env.NODE_ENV,
      timer: null,
      waitTime: 200,
      num: 0,
      maxNum: 10,
      lastTime: new Date().getTime(),
      isNoCode: false,
    };
  },
  computed: {
    ...mapState({
      userInfo: (state) => state.common.userInfo,
      uif: (state) => state.common.uif,
    }),
  },
  methods: {
    goplatform() {
      // 无代码平台
      let userInfo = this.userInfo;
      let urlstr = `${webUrl.nocode}/?tokenParams=`;
      let urlstr1 = "http://localhost:3000/?tokenParams=";
      let tokenParams = {
        uif: this.uif,
        ...userInfo,
        // isApprove: userInfo.isApprove,
        // isPurchase: userInfo.isPurchase,
        // isWork: userInfo.isWork,
        // isBudget: userInfo.isBudget,
        // isInvoiceManager: userInfo.isInvoiceManager,
        // isFinance: userInfo.isFinance,
        // isHumanResource: userInfo.isHumanResource,
        // CompanyId: userInfo.CompanyId,
        // Serial: userInfo.Serial
      };
      urlstr += Base64.encode(JSON.stringify(tokenParams));
      urlstr1 += Base64.encode(JSON.stringify(tokenParams));
      console.log(urlstr1, "urlstr1");
      window.open(urlstr);
    },
    setComponentFun() {
      // 设置iframe页的数组对象
      const componentsArr = this.getComponentsArr();
      componentsArr.forEach((item) => {
        // Vue.component(item.name, item.component);
        Vue.component(item.children[0].name, item.children[0].component);
      });
      this.componentsArr = componentsArr;
    },

    isOpenIframePage() {
      // 根据当前路由设置hasOpen // 遍历路由的所有页面，把含有iframeComponent标识的收集起来
      const target = this.componentsArr.find((item) => {
        return item.path === this.$route.path;
      });
      if (target && !target.children[0].hasOpen) {
        target.children[0].hasOpen = true;
      }

      this.componentsArr.map((item) => {
        // 关闭再次打开重新登录 | 2021 -12-06 | gz
        if (item.children[0].hasOpen) {
          let tag = this.tagList.find((tag) => {
            return tag.value == item.path;
          });
          if (!tag) {
            item.children[0].hasOpen = false;
          }
        }
      });
    },

    getComponentsArr() {
      // 遍历路由的所有页面，把含有iframeComponent标识的收集起来
      const router = this.$router;
      const routes = router.options.routes[0].children;
      const iframeArr = [];
      routes.map((item) => {
        if (item.name.slice(0, 6) == "Layout") {
          let printReportObj = {
            name: item.children[0].name,
            path: item.children[0].path,
            hasOpen: false, // 是否打开过，默认false
            component: item.children[0].iframeComponent, // 组件文件的引用
          };
          item.children.splice(0, 1);
          item.children.push(printReportObj);
          iframeArr.push(item);
        }
      });
      return iframeArr;
    },

    Router() {
      // 跳转消息列表
      this.$router.push({ name: "messageList" });
    },

    setChildren(Children, Childrens) {
      // 设置导航
      if (Children && Children.length != 0 && Childrens) {
        Children.map((i) => {
          Childrens.map((is) => {
            if (i.name == is.name) {
              i.request = is.request;
              i.iconcls = is.iconcls;
              this.setChildren(i.Children, is.Children);
            }
          });
        });
      }
    },

    getNewMsg() {
      // 查询是否有最新消息
      this.$api.messages.NotificationGetNewSingle().then((res) => {
        if (!res.data?.data) {
          return false;
        } else {
          let resData = res.data.data;
          let cTitle = resData.cTitle;
          let cNoticeType = resData.cNoticeType === "1" ? "系统公告" : "系统更新";
          this.$alert(cTitle, cNoticeType, {
            confirmButtonText: "详情",
            callback: (action) => {
              // confirm 详情 | cancel 关闭
              if (action === "confirm") {
                resData.readCount = 0;
                sessionStorage.setItem("itemDetails", JSON.stringify(resData));
                this.$router.push({ name: "messageDetails" });
              } else if (action === "cancel") {
                let alreadyRead = {
                  noticeid: resData.cNoticesID,
                };
                this.$api.messages.AddKnowMsgLog(alreadyRead).then(() => {});
              }
            },
          });
        }
      });
    },
    getNewMsgEnterprise() {
      // 查询是否有最新消息
      this.$api.enterprisePortal.NotReadNotice().then((res) => {
        let resData = res.data.data ? res.data.data[0] : null;
        if (resData == null) {
          return false;
        } else {
          let cTitle = resData.title;
          let cNoticeType = "企业公告";
          this.$alert(cTitle, cNoticeType, {
            confirmButtonText: "详情",
            callback: (action) => {
              // confirm 详情 | cancel 关闭
              if (action === "confirm") {
                this.$router.push({ name: "AnnouncementManagement", query: { id: resData.id } });
              } else if (action === "cancel") {
                this.$api.Announcement.LookKnowNotice({ nid: resData.id }).then((res) => {
                  console.log(res, "cancel");
                });
              }
            },
          });
        }
      });
    },

    getMsgNum() {
      // 获取未读消息数量
      this.$api.messages.NotReadNoticeCount().then((res) => {
        if (res.data.status == 200) {
          this.$store.dispatch("GetMsgNum", res.data.data);
        }
      });
    },

    GetAuthorityTree() {
      // 模板权限树
      this.$api.systemManagement.GetAuthorityTree().then((res) => {
        let resData = res.data.data;
        this.$store.dispatch("getPermissionTree", resData);
      });
    },

    GetWebPageStyle() {
      // 获取登录人当前样式
      this.$api.publicApi.GetWebPageStyle({ userId: `${this.userInfo.UserId}_${this.userInfo.subid}` }).then((res) => {
        let resData = res.data.data;
        if (resData) {
          this.$store.dispatch("setIsEyesHIeld", resData.isProtection);
          this.skin = resData.type;
          this.$store.dispatch("getIslBack", resData.type == 1 ? true : false); // 存储islBack
        }
      });
    },

    getInitData() {
      // 存储基础数据
      GetSubCompanyDataView(this); // 公司部门树
      this.$api.optionsManage.GetEnumDataOptiont({ metaTypeCode: "PM_employeeStatus" }).then((res) => {
        // 员工状态
        let resData = res.data.data;
        this.$store.dispatch("setStaffStateArr", resData);
      });
      this.$api.customFieldApi.GetModuleType().then((res) => {
        // 人事管理模块分组
        let resData = res.data.data;
        this.$store.dispatch("setManagement", resData);
      });
      this.$api.commonXc.GetBankSelect().then((res) => {
        // 获取银行列表
        let resData = res.data.data;
        this.$store.dispatch("setYeepayBank", resData);
      });
    },

    setIsNoCode() {
      this.num++;
      if (this.num >= this.maxNum) this.isNoCode = true;
      setTimeout(() => {
        this.num = 0;
      }, 3000);
    },
  },
};
